package net.huashitong.controller.money;

import huashitong.util.StringUtils;
import io.swagger.annotations.ApiParam;
import net.huashitong.entity.UserMessage;
import net.huashitong.service.money.MoneyService;
import net.huashitong.service.money.SsMoneyOrderService;
import net.huashitong.ssydt.money.entity.SsMoneyOrder;
import net.huashitong.ssydt.money.entity.vo.OrderInVO;
import net.huashitong.ssydt.money.entity.vo.OrderOutVO;
import net.huashitong.ssydt.money.entity.vo.VipBagsVo;
import net.huashitong.ssydt.money.entity.vo.VipMyVO;
import net.huashitong.ssydt.security.utils.Base64Utils;
import net.huashitong.ssydt.security.utils.Result;
import net.huashitong.ssydt.security.utils.ResultUtils;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;

/**
 * vip开通界面
 *
 * @author defang.huang
 */
@Controller
@RequestMapping(value = "/money")
public class MoneyController {

    @Autowired
    private MoneyService moneyService;
    @Autowired
    private SsMoneyOrderService ssMoneyOrderService;

    protected String getView(String view) {
        return String.format("user/%s", view);
    }

    /**
     * vip套餐列表
     */
    @RequestMapping(value = "/vip/myaccount")
    public String getMyVip(Model model) {
        UserMessage userMessage = (UserMessage) SecurityUtils.getSubject().getPrincipal();
        if (userMessage != null) {
            //查看是否vip
            VipMyVO vipMyVO = moneyService.vipMy(userMessage.getUserId());
            model.addAttribute("vipMyVo", vipMyVO);
        }
        //获取vip套餐包
        List<VipBagsVo> vipBagsVoList = moneyService.vipBags();
        model.addAttribute("vipBagsVoList", vipBagsVoList);
        return getView("vip");
    }

    /**
     * 统一下单
     */
    @ResponseBody
    @RequestMapping("/orders")
    public Result<OrderOutVO> orders(@Valid @RequestBody OrderInVO orderInVO) {
        UserMessage userMessage = (UserMessage) SecurityUtils.getSubject().getPrincipal();
        OrderOutVO orderOutVO = moneyService.orders(orderInVO, userMessage.getUserId());
        return ResultUtils.getSuccessResults(orderOutVO);
    }

    /**
     * 发起公众号支付
     */
    @ResponseBody
    @RequestMapping("/orders/{orderId}/wechat/payOrder")
    public Result<TreeMap<String, String>> gzWeChatPay(
            @ApiParam(value = "订单编号", required = true) @PathVariable("orderId") String orderId,
            HttpServletRequest request, HttpServletResponse response) throws Exception {
        TreeMap<String, String> map = moneyService.payForWeChat(orderId, request, response);
        return ResultUtils.getSuccessResults(map);
    }

    /**
     * 获取订单状态
     * @author: huiyang.yu
     * @param orderId
     * @return
     */
    @ResponseBody
    @RequestMapping(value ="/order/state/{orderId}/orderId", method = RequestMethod.POST)
    public Result<String>  orderState(@PathVariable String orderId) {
        orderId = new String(Base64Utils.decode(orderId));
        SsMoneyOrder order = ssMoneyOrderService.getByOrderId(orderId);
        if (order != null && order.getOrderState().equals(SsMoneyOrder.orderState.PAYMENT)) {
            return ResultUtils.getResults("200");
        }
        return ResultUtils.getResults("500");
    }

    /**
     * 发起微信支付 - payForWx(扫码)
     * @author: huiyang.yu
     * @param orderId
     * @param request
     * @param response
     * @return
     * @throws Exception
     */
    @RequestMapping(value ="/order/payForWx/{orderId}")
    public String payForWx(@PathVariable String orderId,Model model, HttpServletRequest request, HttpServletResponse response) throws Exception {
         UserMessage userMessage = (UserMessage) SecurityUtils.getSubject().getPrincipal();
        String userId =userMessage.getUserId();
        String orderId2 = orderId;
        orderId = new String(Base64Utils.decode(orderId));
        Map<String, String> map = moneyService.payWebForWx(orderId, userId, request, response);
        String error = map.get("error");
        if (StringUtils.isNotBlank(error)) {
            model.addAttribute("error", error);
        }
        String code_url = map.get("code_url");
        String money = map.get("money");
        model.addAttribute("money", money);
        model.addAttribute("code_url", code_url);
        model.addAttribute("orderId", orderId2);
        return "pay/wechat";
    }


}
